package cn.net.wanji.job;

import cn.net.wanji.entity.EventCar;
import cn.net.wanji.entity.Gantry;
import cn.net.wanji.mapper.MonitorMapper;
import cn.net.wanji.utils.StringUtils;
import cn.net.wanji.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.net.wanji.job.RefreshGantryDataJob.gantryFlagInfoMap;
import static cn.net.wanji.utils.TimeUtils.calculateMinutesDifference;
import static cn.net.wanji.utils.TimeUtils.isValidDateTimeFormat;

@Slf4j(topic = "定时处理事件列表【ServiceConfigJob】")
//@EnableScheduling
//@Component
public class RefreshEventDataJob {

    @Autowired
    private MonitorMapper monitorMapper;

    @Value("${event.overSpeed}")
    private String overSpeed;

    @Value("${event.lowSpeed}")
    private String lowSpeed;

    @Value("${event.time}")
    private String time;

    @Scheduled(fixedRateString = "${event.checkTime}")
    public void updateEvent() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime currentDateTime = LocalDateTime.now();
        String formattedDateTime = currentDateTime.format(formatter);
        String upGantryId = gantryFlagInfoMap.get("1");
        String downGantryId = gantryFlagInfoMap.get("2");
        List<EventCar> allOptionCar = monitorMapper.getAllOptionCar();
        for (EventCar eventCar : allOptionCar) {
            // 如果是下桥数据 获取对应的上桥数据
            if (downGantryId.equals(eventCar.getErId())) {
                // 获取所有carId和下桥门架一致的数据
                List<EventCar> eventCars = monitorMapper.selectEventInfoByCarId(eventCar.getCarId());
                for (EventCar car : eventCars) {
                    car.setDownBridgeTime(eventCar.getGantryTime());
                    monitorMapper.insertEvent(car);
                }
                //假定数据是下桥数据，则判断上桥数据中，是否存在滞留车辆 更新滞留车辆中的下桥时间
                EventCar eventCar1 = monitorMapper.selectEventByCarId(eventCar.getCarId(), "3");
                if (eventCar1 != null) {
                    // 判断下桥和上桥时间的差值是否大于40分钟
                    long l = calculateMinutesDifference(eventCar1.getUpBridgeTime(), eventCar.getDownBridgeTime());
                    if (l > Integer.parseInt(time)) {
                        eventCar.setEventType("3");
                    } else {
                        eventCar.setEventType("0");
                    }
                    String gantryTime1 = eventCar.getGantryTime();
                    eventCar1.setDownBridgeTime(gantryTime1);
                    monitorMapper.insertEvent(eventCar1);
                }
                eventCar.setDownBridgeTime(eventCar.getGantryTime());
            }
            //首先判断是否是上桥门架
            if (!upGantryId.equals(eventCar.getErId())) {
                // 获取入口门架数据
                EventCar upCar = monitorMapper.selectEventByCarIdAndErId(eventCar.getCarId(), upGantryId);
                // 判断是否有入口信息
                if (upCar != null) {
                    eventCar.setUpBridgeTime(upCar.getGantryTime());
                }
                // 判断是否有速度信息
                if (!StringUtils.isEmpty(eventCar.getSpeed())) {
                    // 判断 低速 超速
                    if (Integer.parseInt(eventCar.getSpeed()) > Integer.parseInt(overSpeed)) {
                        // 获取是否已经有超速事件，如果已经有超速事件，则不进行处理
                        Integer eventNumber = monitorMapper.selectEventCountByCarId(eventCar.getCarId(), "2");
                        if (eventNumber == 0) {
                            eventCar.setEventTime(formattedDateTime);
                            eventCar.setEventNumber(1);
                            eventCar.setEventType("2");
                            eventCar.setTime(formattedDateTime);
                            eventCar.setRecordCarId(eventCar.getRecordId() + eventCar.getCarId() + eventCar.getEventType());
                            monitorMapper.insertEvent(eventCar);
                        } else {
                            eventCar.setEventTime(formattedDateTime);
                            eventCar.setEventNumber(eventNumber + 1);
                            eventCar.setEventType("2");
                            eventCar.setTime(formattedDateTime);
                            eventCar.setRecordCarId(eventCar.getRecordId() + eventCar.getCarId() + eventCar.getEventType());
                            monitorMapper.insertEvent(eventCar);
                        }
                    } else if (Integer.parseInt(eventCar.getSpeed()) < Integer.parseInt(lowSpeed)) {
                        Integer eventNumber = monitorMapper.selectEventCountByCarId(eventCar.getCarId(), "1");
                        if (eventNumber == 0) {
                            eventCar.setEventTime(formattedDateTime);
                            eventCar.setEventNumber(1);
                            eventCar.setEventType("1");
                            eventCar.setTime(formattedDateTime);
                            eventCar.setRecordCarId(eventCar.getRecordId() + eventCar.getCarId() + eventCar.getEventType());
                            monitorMapper.insertEvent(eventCar);
                        } else {
                            eventCar.setEventTime(formattedDateTime);
                            eventCar.setEventNumber(eventNumber + 1);
                            eventCar.setEventType("1");
                            eventCar.setTime(formattedDateTime);
                            eventCar.setRecordCarId(eventCar.getRecordId() + eventCar.getCarId() + eventCar.getEventType());
                            monitorMapper.insertEvent(eventCar);
                        }
                    } else {
                        eventCar.setEventType("0");
                        eventCar.setTime(formattedDateTime);
                        eventCar.setEventTime(formattedDateTime);
                        eventCar.setRecordCarId(eventCar.getRecordId() + eventCar.getCarId() + eventCar.getEventType());
                        monitorMapper.insertEvent(eventCar);
                    }
                } else {
                    eventCar.setEventType("0");
                    eventCar.setTime(formattedDateTime);
                    eventCar.setEventTime(formattedDateTime);
                    eventCar.setRecordCarId(eventCar.getRecordId() + eventCar.getCarId() + eventCar.getEventType());
                    monitorMapper.insertEvent(eventCar);
                }
            } else {
                eventCar.setEventNumber(1);
                if (!StringUtils.isEmpty(eventCar.getSpeed())) {
                    if (Integer.parseInt(eventCar.getSpeed()) > Integer.parseInt(overSpeed)) {
                        eventCar.setEventType("2");
                    } else if (Integer.parseInt(eventCar.getSpeed()) < Integer.parseInt(lowSpeed)) {
                        eventCar.setEventType("1");
                    } else {
                        eventCar.setEventType("0");
                    }
                } else {
                    eventCar.setEventType("0");
                }
                eventCar.setTime(formattedDateTime);
                eventCar.setUpBridgeTime(eventCar.getGantryTime());
                eventCar.setEventTime(formattedDateTime);
                eventCar.setRecordCarId(eventCar.getRecordId() + eventCar.getCarId() + eventCar.getEventType());
                monitorMapper.insertEvent(eventCar);
            }
        }
    }
}
